package com.ltmall.api.manager.impl;

import com.ltmall.api.bean.*;
import com.ltmall.api.dao.IDirectSupplyDAO;
import com.ltmall.api.entity.param.DirectSupplyParam;
import com.ltmall.api.manager.DirectSupplyManager;
import com.ltmall.api.util.PageUtil;
import com.wuwangkeji.homeflavor.enums.DirectSupplyAreaTypeEnum;
import com.wuwangkeji.homeflavor.enums.DirectSupplyStatusEnum;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DirectSupplyManagerImpl implements DirectSupplyManager {

    private IDirectSupplyDAO directSupplyDAO;

    @Override
    public List<ThDirectSupply> getOpenedArea(String curAreaName, DirectSupplyAreaTypeEnum areaType) {

        List<ThDirectSupply> areas = new ArrayList<ThDirectSupply>();

        if ((DirectSupplyAreaTypeEnum.province).equals(areaType)) {
            //获取开通的省
            areas = directSupplyDAO.getOpenedProvince();
        } else if ((DirectSupplyAreaTypeEnum.city).equals(areaType)) {
            //获取开通的市
            areas = directSupplyDAO.getOpenedCityByProvince(curAreaName);
        } else if ((DirectSupplyAreaTypeEnum.county).equals(areaType)) {
            //获取开通的县/区
            areas = directSupplyDAO.getOpenedCountyByCity(curAreaName);
        } else if ((DirectSupplyAreaTypeEnum.town).equals(areaType)) {
            //获取开通的街道
            areas = directSupplyDAO.getOpenedTownByCounty(curAreaName);
        } else if ((DirectSupplyAreaTypeEnum.block).equals(areaType)) {
            //获取开通的街道
            areas = directSupplyDAO.getOpenedBlockByTown(curAreaName);
        }

        return areas;
    }

    @Override
    public List<ThDirectSupply> directSupplyPage(PageUtil page) {
        return directSupplyDAO.directSupplyPage(page);
    }

    @Override
    public List<ThGroupBanners> groupBannersPage(PageUtil page) {
        return directSupplyDAO.groupBannersPage(page);
    }

    @Override
    public List<ThGroupBroadcast> groupBroadcastPage(PageUtil page) {
        return directSupplyDAO.groupBroadcastPage(page);
    }

    @Override
    public List<ThGroupPersonalSetting> groupPersonalSettingPage(PageUtil page) {
        return directSupplyDAO.groupPersonalSettingPage(page);
    }

    @Override
    public boolean openDirectSupply(ThDirectSupply order) {

        boolean flag = false;

        //【1】实体转换
        //ThDirectSupply info = bulidOrder2Do(order);

        //【2】保存
        flag = directSupplyDAO.openDirectSupply(order);

        return flag;
    }

    @Override
    public List<ThGroupBanners> getGroupBannersList(int directSupplyId, int state) {
        return directSupplyDAO.getGroupBannersList(directSupplyId, state);
    }

    @Override
    public List<ThGroupBroadcast> getGroupBroadcastList(int directSupplyId, int state) {
        return directSupplyDAO.getGroupBroadcastList(directSupplyId, state);
    }

    @Override
    public List<ThDirectSupply> getDirectSupplyList() {
        return directSupplyDAO.getDirectSupplyList();
    }

    @Override
    public ThDirectSupply getDirectSupplyById(String id) {
        return directSupplyDAO.getDirectSupplyById(id);
    }

    @Override
    public List<ThDirectSupply> getDirectSupplyByShopId(String shopId) {
        return directSupplyDAO.getDirectSupplyByShopId(shopId);
    }

    @Override
    public List<ThDirectSupply> getDirectSupplyListByName(String area, String areaName) {
        return directSupplyDAO.getDirectSupplyListByName(area, areaName);
    }

    @Override
    public boolean updateDirectSupply(ThDirectSupply directSupply) {
        directSupply.setGmtModified(new Date());
        return directSupplyDAO.updateDirectSupply(directSupply);
    }

    @Override
    public List<ThDirectSupply> getNearbyBlock(double maxLat, double minLat, double maxLng,
                                               double minLng) {
        return directSupplyDAO.getNearbyBlock(maxLat, minLat, maxLng, minLng);
    }

    @Override
    public ThGroupBanners getGroupBanners(int id) {
        return directSupplyDAO.getGroupBanners(id);
    }

    @Override
    public boolean addGroupBanners(ThGroupBanners banners) {
        banners.setState(DirectSupplyStatusEnum.INIT.getCode());
        banners.setOpTime(System.currentTimeMillis());
        return directSupplyDAO.addGroupBanners(banners);
    }

    @Override
    public boolean updateGroupBanners(ThGroupBanners banners) {
        return directSupplyDAO.updateGroupBanners(banners);
    }

    @Override
    public boolean deleteGroupBanners(int id) {
        return directSupplyDAO.deleteGroupBanners(id);
    }

    @Override
    public ThGroupBroadcast getGroupBroadcast(int id) {
        return directSupplyDAO.getGroupBroadcast(id);
    }

    @Override
    public boolean addGroupBroadcast(ThGroupBroadcast groupBroadcast) {
        groupBroadcast.setOptime(System.currentTimeMillis());
        groupBroadcast.setState(DirectSupplyStatusEnum.INIT.getCode());
        return directSupplyDAO.addGroupBroadcast(groupBroadcast);
    }

    @Override
    public boolean updateGroupBroadcast(ThGroupBroadcast groupBroadcast) {
        return directSupplyDAO.updateGroupBroadcast(groupBroadcast);
    }

    @Override
    public List<ThGroupBanners> getGroupBannersListByDirctId(int shopId, int directId) {
        return directSupplyDAO.getGroupBannersListByDirctId(shopId, directId);
    }

    @Override
    public List<ThGroupBroadcast> getGroupBroadcastList(int shopId, String directId) {
        return directSupplyDAO.getGroupBroadcastList(shopId, directId);
    }

    public void setDirectSupplyDAO(IDirectSupplyDAO directSupplyDAO) {
        this.directSupplyDAO = directSupplyDAO;
    }

    @Override
    public ThGroupPersonalSetting getGroupPersonalSetting(int shopId, String directId) {
        return directSupplyDAO.getGroupPersonalSetting(shopId, directId);
    }

    @Override
    public boolean addGroupPersonalSetting(ThGroupPersonalSetting groupPersonalSetting) {
        return directSupplyDAO.addGroupPersonalSetting(groupPersonalSetting);
    }

    @Override
    public boolean updateGroupPersonalSetting(ThGroupPersonalSetting groupPersonalSetting) {
        return directSupplyDAO.updateGroupPersonalSetting(groupPersonalSetting);
    }

    @Override
    public ThGroupPersonalSetting getGroupPersonalSettingByState(int directSupplyId, int state) {
        return directSupplyDAO.getGroupPersonalSettingByState(directSupplyId, state);
    }

    @Override
    public boolean deleteGroupBroadcast(int id) {
        return directSupplyDAO.deleteGroupBroadcast(id);
    }

    @Override
    public boolean saveGroupDeliveryInfo(ThGroupDeliveryAddr deliveryAddr) {
        return directSupplyDAO.saveGroupDeliveryInfo(deliveryAddr);
    }

    @Override
    public ThGroupDeliveryAddr getGroupDeliveryInfo(String userId, String directId) {
        return directSupplyDAO.getGroupDeliveryInfo(userId, directId);
    }

    @Override
    public boolean updateGroupDeliveryInfo(int id, String name, String phone, String address) {
        return directSupplyDAO.updateGroupDeliveryInfo(id, name, phone, address);
    }

    @Override
    public boolean updateGroupPersonalSettingState(int id, int state) {
        return directSupplyDAO.updateGroupPersonalSettingState(id, state);
    }

    @Override
    public boolean updateGroupBroadcastState(int id, int state) {
        return directSupplyDAO.updateGroupBroadcastState(id, state);
    }

    @Override
    public boolean updateGroupBannersState(int id, int state) {
        return directSupplyDAO.updateGroupBannersState(id, state);
    }

    @Override
    public boolean updateDirectSupplyState(int id, int state) {
        return directSupplyDAO.updateDirectSupplyState(id, state);
    }

    @Override
    public ThGroupDeliveryAddr getGroupDeliveryInfoById(int id) {
        return directSupplyDAO.getGroupDeliveryInfoById(id);
    }

    /**
     * 实体转换
     * 
     * @param order
     * @return
     */
    private ThDirectSupply bulidOrder2Do(DirectSupplyParam order) {
        ThDirectSupply info = new ThDirectSupply();
        info.setProvince(order.getProvince());
        info.setCity(order.getCity());
        info.setCounty(order.getCounty());
        info.setTown(order.getTown());

        info.setBlockAddress(order.getBlockAddress());
        info.setBlockContactNumber(order.getBlockContactNumber());
        info.setBlockLinkman(order.getBlockLinkman());
        info.setBlockName(order.getBlockName());
        info.setPickupPoint(order.getPickupPoint());
        return info;
    }

}
